/**
 * 
 */
package info.niwota.webshot;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {

	private static final String CAPTURE_OUTPUT_FORMAT = "capture.output.format";

	private static final String CAPTURE_QUALITY = "capture.quality";

	private static final String CAPTURE_BITMAP_CONFIG = "capture.bitmap.config";
	
	private static final String CAPTURE_SMART_TOGGLE = "capture.smart.toggle";

	private static final String CAPTURE_BUTTON_TOGGLE = "capture.button.toggle";

	private static final String CAPTURE_OUTPUT_FORMAT_DEFAULT = ".jpg";

	private static final int CAPTURE_QUALITY_DEFAULT = 100;

	private static final int CAPTURE_BITMAP_CONFIG_DEFAULT = 2; //argb-8888

	public static final Object EXT_JPG = ".jpg";
	
	public static final Object EXT_PNG = ".png";

	private static final String CAPTURE_BUTTON_POSITION = "capture.button.position";

	private static final String SEP = ",";
	
	private static final String NAVI_BAR_TOGGLE = "navi.bar.toggle";
	
	private static final String FULLSCREEN_TOGGLE = "fullscreen.toggle";
	
	private static final String ZOOM_VOL_TOGGLE = "zoom.vol.toggle";

	public static String getOutputFormat(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(CAPTURE_OUTPUT_FORMAT, null);
			if (!TextUtils.isEmpty(s)) {
				return s;
			}
		} catch (Exception e) {
		}
		return CAPTURE_OUTPUT_FORMAT_DEFAULT;
	}
	
	public static int[] getLastButtonPosition(Context ctx) {
		int[] pos = new int[2];
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(CAPTURE_BUTTON_POSITION, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(SEP);
				pos[0] = Integer.parseInt(a[0]);
				pos[1] = Integer.parseInt(a[1]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pos;
	}
	
	public static void putLastButtonPosition(Context ctx, int x, int y) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		String s = x + SEP + y;
		p.edit().putString(CAPTURE_BUTTON_POSITION, s).commit();
	}
	
	public static int getQuality(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(CAPTURE_QUALITY, null);
			if (!TextUtils.isEmpty(s)) {
				int q = Integer.parseInt(s);
				return (q < 1 || q > 100 ? CAPTURE_QUALITY_DEFAULT : q);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return CAPTURE_QUALITY_DEFAULT;
	}

	public static int getBitmapConfig(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(CAPTURE_BITMAP_CONFIG, null);
			if (!TextUtils.isEmpty(s)) {
				int c = Integer.parseInt(s);
				return (c < 0 || c > 3 ? CAPTURE_BITMAP_CONFIG_DEFAULT : c);
			}
		} catch (Exception e) {
		}
		return CAPTURE_BITMAP_CONFIG_DEFAULT;
	}

	public static boolean isSmartCapture(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(CAPTURE_SMART_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	
	public static boolean isShowCaptureButton(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(CAPTURE_BUTTON_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	
	public static boolean isShowNaviBar(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(NAVI_BAR_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	
	public static boolean isFullscreen(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(FULLSCREEN_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	
	public static boolean isZoomVol(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(ZOOM_VOL_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}
	//
	private Preferences() {
	}
}
